package com.ruoyi.wb.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbContractUser;
import com.ruoyi.wb.domain.WbMaintainCompany;
import com.ruoyi.wb.mapper.WbContractUserMapper;
import com.ruoyi.wb.service.IWbContractUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 合同用户关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-25
 */
@Service
public class WbContractUserServiceImpl implements IWbContractUserService
{
    @Autowired
    private WbContractUserMapper wbContractUserMapper;

    /**
     * 查询合同用户关联
     * 
     * @param id 合同用户关联主键
     * @return 合同用户关联
     */
    @Override
    public WbContractUser selectWbContractUserById(Long id)
    {
        return wbContractUserMapper.selectWbContractUserById(id);
    }

    /**
     * 查询合同用户关联列表
     * 
     * @param wbContractUser 合同用户关联
     * @return 合同用户关联
     */
    @Override
    public List<WbContractUser> selectWbContractUserList(WbContractUser wbContractUser)
    {
        return wbContractUserMapper.selectWbContractUserList(wbContractUser);
    }

    /**
     * 新增合同用户关联
     * 
     * @param wbContractUser 合同用户关联
     * @return 结果
     */
    @Override
    public int insertWbContractUser(WbContractUser wbContractUser)
    {
        wbContractUser.setCreateTime(DateUtils.getNowDate());
        return wbContractUserMapper.insertWbContractUser(wbContractUser);
    }

    /**
     * 修改合同用户关联
     * 
     * @param wbContractUser 合同用户关联
     * @return 结果
     */
    @Override
    public int updateWbContractUser(WbContractUser wbContractUser)
    {
        wbContractUser.setUpdateTime(DateUtils.getNowDate());
        return wbContractUserMapper.updateWbContractUser(wbContractUser);
    }

    /**
     * 批量删除合同用户关联
     * 
     * @param ids 需要删除的合同用户关联主键
     * @return 结果
     */
    @Override
    public int deleteWbContractUserByIds(Long[] ids)
    {
        return wbContractUserMapper.deleteWbContractUserByIds(ids);
    }

    /**
     * 删除合同用户关联信息
     * 
     * @param id 合同用户关联主键
     * @return 结果
     */
    @Override
    public int deleteWbContractUserById(Long id)
    {
        return wbContractUserMapper.deleteWbContractUserById(id);
    }


    /**
     * 批量新增合同用户关联
     *
     * @param wbContractUsers 批量新增合同用户关联
     * @return 结果
     */
    @Override
    public int insertWbContractUserList(List<WbContractUser> wbContractUsers) {

        if (!CollectionUtils.isEmpty(wbContractUsers)){
            List<String> Phones= wbContractUsers.stream().map(WbContractUser::getUserPhone).collect(Collectors.toList());
            Set<String> Phones2= wbContractUsers.stream().map(WbContractUser::getUserPhone).collect(Collectors.toSet());
            if (!(Phones.size()==Phones2.size())){
                throw  new RuntimeException("存在重复的手机号请检查！");
            }
            //先查询当前企业有没有相同手机号
            List<WbContractUser> wbContractUserList=wbContractUserMapper.selectWbContractUserListByPhoneNumbers(wbContractUsers.get(0).getContractId(),Phones);
            if (!CollectionUtils.isEmpty(wbContractUserList)){
                String[] strings = wbContractUserList.stream().map(WbContractUser::getUserPhone).toArray(String[]::new);
                String join = ArrayUtil.join(strings, " , ");
                throw  new RuntimeException("当前合同已存在以下手机号 : "+join);
            }
        }
        wbContractUsers.forEach(item->{
            item.setCreateTime(DateUtils.getNowDate());
        });

        return  wbContractUserMapper.insertWbContractUsers(wbContractUsers);
    }

    /**
     * 查询用户合作伙伴
     */
    @Override
    public List<WbMaintainCompany> selectPartner() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        return wbContractUserMapper.selectPartner(sysUser.getPhonenumber());
    }
}
